package 链式栈;

import 栈.IStack;

public class LinkStack implements IStack {
    Node top;//栈顶节点
    int size;//节点个数
    //初始化
    public LinkStack(){

    }


    @Override
    public void push(Object obj) throws Exception {
        Node p = new Node(obj,null);
        p.next=top;
        top=p;
        size++;
    }

    @Override
    public Object pop() throws Exception {
        if(top==null) {
            return null;
        }else{
            Object e = top.data;
            top=top.next;
            size++;
            return e;
        }

    }

    @Override
    public Object getTop() throws Exception {
        if(top==null)
            return null;
        return top.data;
    }

    @Override
    public boolean isEmpty() {
        return top==null;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public void print() {
            Node curr =  top;
            while(curr!=null){
                System.out.println(curr.data+"");
                curr=curr.next;
            }
        System.out.println();
    }
}
